package com.bw.wxy.fragment;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.wxy.R;
import com.bw.wxy.common.LogUtils;
import com.bw.wxy.db.UserManager;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.mvp.base.view.BaseFragment;
import com.bw.wxy.mvp.contract.LREContract;
import com.bw.wxy.mvp.di.DaggerLREComponent;
import com.bw.wxy.mvp.di.LREModules;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.ChannelEntity;
import com.bw.wxy.mvp.model.entity.NewsEntity;
import com.bw.wxy.mvp.model.entity.TextBannerEntity;
import com.bw.wxy.mvp.model.entity.UserEntity;
import com.bw.wxy.mvp.presenter.LREPresenter;
import com.bw.wxy.mvp.view.news.adapter.NewsVpAdapter;
import com.bw.wxy.mvp.view.news.adapter.TextBannerAdapter;
import com.flyco.tablayout.SlidingTabLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fragment_news extends BaseFragment<LREPresenter> implements LREContract.ILREView,
        OnBannerListener<TextBannerEntity.Values> {

    private Banner newsTextBanner;
    private SlidingTabLayout newsTab;
    private ViewPager newsVp;
    private TextBannerAdapter textBannerAdapter;
    private List<Fragment> fragments;
    private Fragment_News_Content fragment_news_content;
    private NewsVpAdapter newsVpAdapter;

//    @Override
//    public int addBarColor() {
//        return Color.WHITE;
//    }
//
//    @Override
//    public View addStatusView() {
//        return f(R.id.news_linear);
//    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_news;
    }

    @Override
    public void initView() {
        newsTextBanner = f(R.id.news_text_banner);
        newsTab = f(R.id.news_tab);
        newsVp = f(R.id.news_vp);
    }

    @Override
    public void initData() {
        fragments = new ArrayList<>();
        List<UserEntity> list = UserManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
        boolean flag = list.size() == 0;

        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.TEXTBANNERCODE);
        Map<String, Object> channelMap = new HashMap<>();
        channelMap.put("code", HttpCode.USERCHANNELCODE);
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        channelMap.put("imie", flag ? PhoneUtils.getIMEI() : "");
        channelMap.put("userId", flag ? 0 : 1);
        p.requestAll(bannerMap, channelMap);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build().lreData(this);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
//        com.blankj.utilcode.util.LogUtils.json(baseEntity);
        if (baseEntity instanceof TextBannerEntity) {
            //文字banner返回
            TextBannerEntity textBannerEntity = (TextBannerEntity) baseEntity;
            LogUtils.e("textBannerEntity:" + textBannerEntity.values);
            if (textBannerEntity.getValues() == null) {
                ToastUtils.showShort("文字广告数据丢失");
                return;
            }
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            newsTextBanner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer())
                    .setOnBannerListener(this);
        } else if (baseEntity instanceof ChannelEntity) {
            //频道列表数据
            //按照频道数量以及频道id初始化fragments
            ChannelEntity channelEntity = (ChannelEntity) baseEntity;
            if (channelEntity.getValues() == null) {
                ToastUtils.showShort("无频道数据");
                return;
            }
            for (int i = 0; i < channelEntity.getValues().size(); i++) {
                fragments.add(new Fragment_News_Content(channelEntity.getValues().get(i).channel_id));
            }
            newsVpAdapter = new NewsVpAdapter(getFragmentManager(), fragments, channelEntity.getValues());
            newsVp.setAdapter(newsVpAdapter);
            newsTab.setViewPager(newsVp);
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {

    }

    @Override
    public void onStart() {
        super.onStart();
        newsTextBanner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        newsTextBanner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        newsTextBanner.destroy();
    }

    /**
     * 点击事件
     *
     * @param data     数据实体
     * @param position 当前位置
     */
    @Override
    public void OnBannerClick(TextBannerEntity.Values data, int position) {
        ToastUtils.showShort("点击广告:" + data.news_title);
    }
}